package cn.kgc.news.controller;

import cn.kgc.news.pojo.News;
import cn.kgc.news.service.NewsService;
import cn.kgc.news.service.impl.NewsServiceImpl;
import com.alibaba.fastjson.JSON;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@WebServlet(name = "NewsServlet",urlPatterns = "/newsServlet")
public class NewsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String action=request.getParameter("action");
        PrintWriter out=response.getWriter();
      NewsService newsService=new NewsServiceImpl();
      if("select".equals(action)) {
          List<News> list = newsService.getNewsList();
          StringBuffer jsonNews=new StringBuffer("[");
          for(int i=0;i<list.size();i++){
              jsonNews.append("{\"id\":"+list.get(i).getId()+",\"title\":\""+list.get(i).getTitle()+"\",\"author\":\""+list.get(i).getAuthor()+"\",\"createDate\":\""+list.get(i).getCreateDate()+"\"}");
              if(i!=list.size()-1){
                  jsonNews.append(",");
              }
          }
          jsonNews.append("]");
          out.print(jsonNews.toString());
      }else  if("select3".equals(action)) {
          List<News> list = newsService.getNewsList();
          String jsonNews= JSON.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
          out.print(jsonNews);
      }
      else  if("select1".equals(action)) {
          List<News> list = newsService.getNewsList();
          StringBuffer newsList=new StringBuffer("");
          for (int i = 0; i <list.size() ; i++) {
              newsList.append("<tr>" +
                      "<td>"+list.get(i).getTitle()+"</td>" +
                      "<td>"+list.get(i).getAuthor()+"</td>" +
                      "<td>"+list.get(i).getCreateDate()+"</td>" +
                      "<td>"+
                      "<a href='newsDetailUpdateSimple.jsp?id='"+list.get(i).getId()+">编辑</a>" +
                      "<a href='#'>删除</a>"+
                      "</td>" +
                      "</tr>");
          }
          out.print(newsList);
      }else if("select2".equals(action)){
          List<News> list = newsService.getNewsList();
          request.setAttribute("list", list);
          request.getRequestDispatcher("jsp/showNews.jsp").forward(request, response);
      }else if("add".equals(action)){
          int categoryId=Integer.parseInt(request.getParameter("categoryId"));
          String summary=request.getParameter("summary");
          String content=request.getParameter("content");
          String author=request.getParameter("author");
          String picPath=request.getParameter("picPath");
          String title=request.getParameter("title");
          News news=new News();
          news.setCategoryId(categoryId);
          news.setAuthor(author);
          news.setContent(content);
          news.setSummary(summary);
          news.setTitle(title);
          news.setCreateDate( new Date());
          news.setModifyDate(new Date());
          news.setPicPath(picPath);
          int n=newsService.insertNews(news);
          boolean flag=false;
          if(n>0){
              flag=true;
          }
          String result="{\"flag\":\""+flag+"\"}";
          out.print(result);
      }else if("delete".equals(action)){
          int id=Integer.parseInt(request.getParameter("id"));
          int n=newsService.deleteNews(id);
          boolean flag=false;
          if(n>0){
              flag=true;
          }
          String result="{\"flag\":\""+flag+"\"}";
          out.print(result);
      }else if("getNewsById".equals(action)){
          int id=Integer.parseInt(request.getParameter("id"));
          News news=newsService.getNewsById(id);
          String newsJson=JSON.toJSONString(news);
          out.print(newsJson);
      }else if("getDetailById".equals(action)){
          int id=Integer.parseInt(request.getParameter("id"));
          News news=newsService.getNewsById(id);
          request.setAttribute("news",news);
          request.getRequestDispatcher("jsp/newsDetailView.jsp").forward(request,response);

      } else if("update".equals(action)){
          int id=Integer.parseInt(request.getParameter("id"));
          int categoryId=Integer.parseInt(request.getParameter("categoryId"));
          String summary=request.getParameter("summary");
          String content=request.getParameter("newscontent");
          String author=request.getParameter("author");
          String picPath=request.getParameter("picPath");
          String title=request.getParameter("title");
          News news=new News();
          news.setId(id);
          news.setCategoryId(categoryId);
          news.setAuthor(author);
          news.setContent(content);
          news.setSummary(summary);
          news.setTitle(title);
          news.setCreateDate( new Date());
          news.setModifyDate(new Date());
          news.setPicPath(picPath);
          int n=newsService.updateNews(news);
          boolean flag=false;
          if(n>0){
              flag=true;
          }
          response.sendRedirect("/newsServlet?action=select&flag="+flag);
      }
      out.flush();
      out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }
}
